package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.Mapper.CommodityMapper;
import com.example.demo.Mapper.CooperativeEntMapper;
import com.example.demo.base.ArrayCountUtils;
import com.example.demo.base.Result;
import com.example.demo.entity.*;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/cooperativeEnt")
public class CooperativeEntController {
    @Resource
    CooperativeEntMapper cooperativeEntMapper;

    /**
     * 获取数据库全部数据
     *
     * @return
     */
    @GetMapping("/all")
    public Result<?> findAll() {
        return Result.success(cooperativeEntMapper.selectList(null));
    }

    /**
     * 分页获取数据库数据
     *
     * @param pageNum
     * @param pageSize
     * @return
     */

    @GetMapping(value = "/pageAll")
    public Result<?> findPageF(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize,
                               @RequestParam Integer tenantId) {
        LambdaQueryWrapper<CooperativeEnt> wrapper = Wrappers.<CooperativeEnt>lambdaQuery();
        wrapper.eq(CooperativeEnt::getTenantId, Integer.valueOf(tenantId));
        Page<CooperativeEnt> cooperativeEntPage = cooperativeEntMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(cooperativeEntPage);
    }

    /**
     * 根据ID精确查找
     *
     * @param firmId
     * @return
     */
    @GetMapping("/findByfirmId")
    public Result<?> findByfirmId(@RequestParam String firmId, @RequestParam String tenantId) {
        CooperativeEnt cooperativeEnt = cooperativeEntMapper.selectById(firmId);
        List<CooperativeEnt> list = new ArrayList<>();
        if (cooperativeEnt != null) {
            list.add(cooperativeEnt);
            return Result.success(list);
        }
        return Result.error(0, "没有查询到相关数据");
    }

    /**
     * 根据企业名称模糊查找
     *
     * @param name
     * @return
     */
    @GetMapping("/findByName")
    public Result<?> findByName(@RequestParam String name,  @RequestParam("tenantId") String tenantId) {
        LambdaQueryWrapper<CooperativeEnt> wrapper = Wrappers.<CooperativeEnt>lambdaQuery();
        wrapper.like(CooperativeEnt::getFirmName, name).eq(CooperativeEnt::getTenantId, tenantId);
        List<CooperativeEnt> list = cooperativeEntMapper.selectList(wrapper);
        if (list.size() > 0)
            return Result.success(list);
        return Result.error(0, "没有查询到相关数据");
    }


    /**
     * 删除
     *

     * @return
     */
    @DeleteMapping("/delById")
    public Result<?> delete(@RequestParam String firmId,
                            @RequestParam String tenantId) {
        CooperativeEnt cooperativeEnt=new CooperativeEnt();
        cooperativeEnt.setFirmId(Integer.valueOf(firmId));
        cooperativeEnt.setTenantId(Integer.valueOf(tenantId));
        int res = cooperativeEntMapper.deleteByMultiId(cooperativeEnt);
        if (res > 0)
            return Result.success();
        return Result.error(0, "删除失败");
    }
    /**
     * 生成农产品合作企业来源地统计
     * @return
     */


    @GetMapping("/pieChatData")
    public Result<?> chatData(@RequestParam String tenantId) {
        QueryWrapper<CooperativeEnt> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tenant_id", Integer.valueOf(tenantId));
        List<CooperativeEnt> CooperativeEntList = cooperativeEntMapper.selectList(queryWrapper);
        String[] res = new String[CooperativeEntList.size()];
        for(int i = 0; i < CooperativeEntList.size(); i++)
        {
            res[i] = CooperativeEntList.get(i).getFirmName().substring(0,2);
        }
        ArrayCountUtils arrayCountUtils = new ArrayCountUtils();
        List<ArrayCountUtils.Group> groups = arrayCountUtils.find(res);
        return Result.success(groups);
    }
    /**
     * 添加信息
     * @param firmId
     * @param firmName
     * @param needPro
     * @param name
     * @param tel
     * @param location
     * @param note
     * @return
     */
    @GetMapping(value = "/add")
    public Result<?> saveC(@RequestParam String firmId,
                           @RequestParam String firmName,
                           @RequestParam String needPro,
                           @RequestParam String name,
                           @RequestParam String tel,
                           @RequestParam String location,
                           @RequestParam String note,
                           @RequestParam String tenantId) {
        if (firmId.isEmpty() || needPro.isEmpty() || name.isEmpty() || tel.isEmpty() || firmName.isEmpty() || location.isEmpty()) {
            return Result.error(0, "文本不能为空");
        }
        CooperativeEnt cooperativeEnt = new CooperativeEnt(Integer.valueOf(firmId), firmName, needPro, name, tel, location, note,Integer.valueOf(tenantId));
        cooperativeEntMapper.insert(cooperativeEnt);
        return Result.success();
    }

    /**
     * 修改信息
     * @param firmId
     * @param firmName
     * @param needPro
     * @param name
     * @param tel
     * @param location
     * @param note
     * @return
     */
    @GetMapping(value = "/update")
    public Result<?> updateByfirmId(@RequestParam String firmId,
                           @RequestParam String firmName,
                           @RequestParam String needPro,
                           @RequestParam String name,
                           @RequestParam String tel,
                           @RequestParam String location,
                           @RequestParam String note,
                                    @RequestParam String tenantId) {
        if (firmId.isEmpty() || needPro.isEmpty() || name.isEmpty() || tel.isEmpty() || firmName.isEmpty() || location.isEmpty()) {
            return Result.error(0, "文本不能为空");
        }
        CooperativeEnt cooperativeEnt = new CooperativeEnt(Integer.valueOf(firmId), firmName, needPro, name, tel, location, note,Integer.valueOf(tenantId));
        cooperativeEntMapper.updateByMultiId(cooperativeEnt);
        return Result.success();
    }

}
